package v2.com.autoresource.service;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import v2.com.autoresource.mapper.TbOrderMapper;
import v2.com.autoresource.model.dao.TbOrder;


import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @ProjectName: learning
 * @Package: com.autoresource.service
 * @ClassName: OrderService
 * @Description:
 * @Date: 2021/11/3 3:52 下午
 * @Version: 1.0
 */
@Service
public class OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;

    private Random random = new Random();

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public List<TbOrder> batchQueryByIds(long id, int size){
        return tbOrderMapper.batchQueryByIds(id,size);
    }


    public void batchInsertOrder() {
        SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
        TbOrderMapper mapper = sqlSession.getMapper(TbOrderMapper.class);
        for (int i = 0; i < 10000000; i++) {
            TbOrder tbOrder = buildTbOrder();
            mapper.insertSelective(tbOrder);
            System.out.println(tbOrder);
            if (i % 10000 == 0) {
                sqlSession.commit();
                sqlSession.clearCache();
            }
        }
        System.out.println(mapper);

    }


    public void insertOrder() {
        for (int i = 0; i < 10000000; i++) {
            TbOrder tbOrder = buildTbOrder();
            tbOrderMapper.insertSelective(tbOrder);
            System.out.println(tbOrder);
        }
        System.out.println(tbOrderMapper);
    }

    private TbOrder buildTbOrder() {
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderNo(UUID.randomUUID().toString());
        tbOrder.setUserId((long) UUID.randomUUID().toString().hashCode());
        tbOrder.setShopId(random.nextLong());

        tbOrder.setPaymentChannel(1);
        tbOrder.setOrderStatus(2);
        tbOrder.setCreateTime(new Date());
        tbOrder.setPaymentTime(new Date());
        tbOrder.setOrderSn(UUID.randomUUID().toString());
        tbOrder.setGoodsCount(1);
        tbOrder.setGoodsAmount(100L);
        tbOrder.setActualPayAmount(100L);

        tbOrder.setLogisticsFee(0L);
        tbOrder.setAddressId(random.nextLong());
        tbOrder.setDeliveryTime(new Date());
        tbOrder.setCompletedTime(new Date());
        return tbOrder;
    }
}
